package com.xysq.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.rockcent.model.constant.ModelType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.activity.QRCodeActivity;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.ViewFindUtils;

/**
 * Created by Steward_Han on 2015/9/23.
 */
public class CustomBaseDialog extends WeiXinBaseDialog {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private ImageView img_minus;
    private ImageView img_plus;
    private EditText numberCountEdit;
    private int count;
    private int id;
    private String name;
    private int isDelivery;
    private ModelType modelType;
    private int couponNumber;
    private Context context;

    public CustomBaseDialog(Context context, int couponNumber, int id, String name, int isDelivery, ModelType modelType) {
        super(context);
        this.context = context;
        this.couponNumber = couponNumber;
        this.id = id;
        this.name = name;
        this.isDelivery = isDelivery;
        this.modelType = modelType;
    }

    @Override
    public View onCreateView() {
        widthScale(0.85f);
        View inflate = View.inflate(context, R.layout.dialog_custom_base, null);
        tv_cancel = ViewFindUtils.find(inflate, R.id.txt_cancle);
        tv_confirm = ViewFindUtils.find(inflate, R.id.txt_confirm);
        img_plus = ViewFindUtils.find(inflate, R.id.img_plus);
        img_minus = ViewFindUtils.find(inflate, R.id.img_minus);
        numberCountEdit = ViewFindUtils.find(inflate, R.id.edit_number_count);
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#ffffff"), dp2px(5)));
        setCanceledOnTouchOutside(false);
        return inflate;
    }

    @Override
    public boolean setUiBeforShow() {
        img_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCount();
                if (count > 1) {
                    numberCountEdit.setText(String.valueOf(count - 1));
                }
            }
        });

        img_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCount();
                if (count < couponNumber) {
                    numberCountEdit.setText(String.valueOf(count + 1));
                }
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(context, "1021");
                checkCount();
                dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                dismiss();
                MobclickAgent.onEvent(context, "1020");
                checkCount();
                if (!numberCountEdit.getText().toString().equals("")) {
                    if (count <= couponNumber & count > 0) {
                        Intent intent = new Intent(context, QRCodeActivity.class);
                        intent.putExtra("count", count);
                        intent.putExtra("id", id);
                        intent.putExtra("name", name);
                        intent.putExtra("isDelivery", isDelivery);
                        intent.putExtra("modelType", modelType);
                        context.startActivity(intent);
                        dismiss();
                    } else {
                        Log.e("number", "count:" + count + " couponNumber: " + couponNumber);
                        ToastUtil.showLong(context, R.string.toast_un_enough_coupon);
                    }
                } else {
                    ToastUtil.showShort(context, R.string.toast_correct_number);
                }
            }
        });

        return false;
    }

    private void checkCount(){
        if (!numberCountEdit.getText().toString().equals(null) & !numberCountEdit.getText().toString().equals("")) {
            count = Integer.parseInt(numberCountEdit.getText().toString());
        } else {
            count = 0
            ;
        }
    }
}